package com.cl.ks.controller;

import com.alibaba.fastjson.JSONArray;
import com.cl.ks.entity.KsFilter;
import com.cl.ks.entity.KsRule;
import com.cl.ks.service.KsRuleServiceOld;
import com.cl.ks.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author chenyitao
 * @date 2021/4/20
 */
@RestController
@RequestMapping("/rule")
@CrossOrigin
@Api(tags = "规则接口")
public class KsRuleControllerOld {
    private final KsRuleServiceOld ksRuleServiceOld;

    public KsRuleControllerOld(KsRuleServiceOld ksRuleServiceOld) {
        this.ksRuleServiceOld = ksRuleServiceOld;
    }

    @RequestMapping(path = "/exportKsRules", method = RequestMethod.GET)
    public CommonResult<JSONArray>  exportKsRules(@RequestParam("ids") List<Integer> ids) {
        return CommonResult.success(ksRuleServiceOld.exportKsRules(ids));
    }

    @RequestMapping(path = "/exportKsFilters", method = RequestMethod.GET)
    public CommonResult<JSONArray> exportKsFilters(@RequestParam("ids") List<Integer> ids) {
        return CommonResult.success(ksRuleServiceOld.exportKsFilters(ids));
    }

    @RequestMapping(path = "/importKsRules", method = RequestMethod.POST)
    public CommonResult<List<KsRule>> importKsRules(@RequestBody String ksRulesJson) {
        return CommonResult.success(ksRuleServiceOld.importKsRules(ksRulesJson));
    }

    @RequestMapping(path = "/importKsFilters", method = RequestMethod.POST)
    public CommonResult<List<KsFilter>> importKsFilters(@RequestBody String ksFiltersJson) {
        return CommonResult.success(ksRuleServiceOld.importKsFilters(ksFiltersJson));
    }


    @ApiOperation("获取数据源可用的规则列表")
    @RequestMapping(path = "/listAvailableRuleBySourceTableId", method = RequestMethod.GET)
    public ResponseEntity<Object> listAvailableRuleBySourceTableId(Integer sourceTableId) {
        return ksRuleServiceOld.listAvailableRuleBySourceTableId(sourceTableId);
    }


    @ApiOperation("获取规则需要的数据模板")
    @RequestMapping(path = "/getDataSourceTemplate", method = RequestMethod.GET)
    public ResponseEntity<Object> getDataSourceTemplate(Integer ruleId) {
        return ksRuleServiceOld.getDataSourceTemplate(ruleId);
    }

    @ApiOperation("获取规则需要的数据模板Excel")
    @RequestMapping(path = "/getDataSourceTemplateExcel", method = RequestMethod.GET)
    public ResponseEntity<Object> getDataSourceTemplateExcel(Integer ruleId, HttpServletResponse response) {
        return ksRuleServiceOld.getDataSourceTemplateExcel(ruleId, response);
    }

    @ApiOperation("获取数据类型可用的规则列表")
    @RequestMapping(path = "/listAvailableRuleByDataTypeCode", method = RequestMethod.GET)
    public ResponseEntity<Object> listAvailableRuleByDataTypeCode(String dataTypeCode) {
        return ksRuleServiceOld.listAvailableRuleByDataTypeCode(dataTypeCode);
    }


    @ApiOperation("获取表的字段列表")
    @RequestMapping(path = "/fields", method = RequestMethod.GET)
    public ResponseEntity<Object> getFields(@RequestParam(name = "table", defaultValue = "") String tableName) {
        return ksRuleServiceOld.getFields(tableName);
    }


    @ApiOperation("获取规则运算符")
    @RequestMapping(path = "/ops", method = RequestMethod.GET)
    public ResponseEntity<Object> getOps() {
        return ksRuleServiceOld.getOps();
    }

    @ApiOperation("获取规则函数")
    @RequestMapping(path = "func", method = RequestMethod.GET)
    public ResponseEntity<Object> getFunc() {
        return ksRuleServiceOld.getFunc();
    }

    @ApiOperation("新增规则")
    @RequestMapping(path = "/add", method = RequestMethod.POST)
    public ResponseEntity<Object> addKsRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody KsRuleServiceOld.KsRuleParam ksRuleParam) {
        return ksRuleServiceOld.addKsRule(auth, ksRuleParam);
    }

    @ApiOperation("编辑规则")
    @RequestMapping(path = "/edit", method = RequestMethod.POST)
    public ResponseEntity<Object> editKsRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody KsRuleServiceOld.KsRuleParam ksRuleParam) {
        return ksRuleServiceOld.editKsRule(auth, ksRuleParam);
    }

    @ApiOperation("删除规则")
    @RequestMapping(path = "/del", method = RequestMethod.GET)
    public ResponseEntity<Object> delKsRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestParam(name = "id") Integer id) {
        return ksRuleServiceOld.delKsRule(auth, id);
    }


    @ApiOperation("查询规则对应表")
    @RequestMapping(path = "/tables", method = RequestMethod.GET)
    public ResponseEntity<Object> rlRuleKsTables(@RequestParam(name = "id") Integer id) {
        return ksRuleServiceOld.rlRuleKsTables(id);
    }

    @RequestMapping(path = "/class/data", method = RequestMethod.GET)
    public ResponseEntity<Object> getClassFiledData(@RequestParam(name = "tableName") String tableName,
                                                    @RequestParam(name = "fieldName") String fieldName, @RequestParam(name = "dbCode") String dbCode) {
        return ksRuleServiceOld.getClassFiledData(tableName, fieldName, dbCode);
    }

    @RequestMapping(path = "/class/update", method = RequestMethod.GET)
    public ResponseEntity<Object> updateClassFiledData(@RequestParam(name = "tableName") String tableName,
                                                       @RequestParam(name = "fieldName") String fieldName, @RequestParam(name = "dbCode") String dbCode) {
        return ksRuleServiceOld.updateClassFiledData(tableName, fieldName, dbCode);
    }

    @ApiOperation("快捷创建提取任务")
    @RequestMapping(path = "/createFilter", method = RequestMethod.GET)
    public ResponseEntity<Object> createFilter(@RequestHeader(name = "Authorization", defaultValue = "") String auth, Integer ruleId) {
        ksRuleServiceOld.createFilter(auth, ruleId);
        return ResponseEntity.ok("success");
    }

    @ApiOperation("使用数据源和规则快捷创建提取任务")
    @RequestMapping(path = "/createFilterByRuleTables", method = RequestMethod.POST)
    public ResponseEntity<Integer> createFilter(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody KsRuleServiceOld.KsRuleParam ksRuleParam) {
        return ksRuleServiceOld.createFilter(auth, ksRuleParam);
    }
    @ApiOperation("使用数据源和规则快捷创建提取任务")
    @RequestMapping(path = "/batchCreateFilterByRuleTables", method = RequestMethod.POST)
    public ResponseEntity<Integer> batchCreateFilterByRuleTables(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody KsRuleServiceOld.KsRuleParam ksRuleParam) {
        ksRuleServiceOld.createFilters(auth, ksRuleParam);
        return null;
    }


    @ApiOperation("批量快捷创建提取任务")
    @RequestMapping(path = "/createFilters", method = RequestMethod.GET)
    public ResponseEntity<Object> createFilters(@RequestHeader(name = "Authorization", defaultValue = "") String auth, String ruleIds) {
        String msg = "over ";
        for (String ruleId : ruleIds.split(",")) {
            try {
                ksRuleServiceOld.createFilter(auth, Integer.valueOf(ruleId));
            } catch (Exception e) {
                e.printStackTrace();
                msg += e.getMessage() + " \n ";
            }
        }
        return ResponseEntity.ok(msg);
    }

    @ApiOperation("查询规则列表")
    @RequestMapping(path = "/getKsRuleList", method = RequestMethod.POST)
    public ResponseEntity<Object> getKsRuleListNew(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody Map<String, Object> param) {
        return ksRuleServiceOld.getKsRuleListNew(auth, param);
    }
    @ApiOperation("查询规则列表")
    @RequestMapping(path = "/list", method = RequestMethod.POST)
    public ResponseEntity<Object> getKsRuleList(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody Map<String, Object> param) {
        return ksRuleServiceOld.getKsRuleList(auth, param);
    }

    @ApiOperation("查询规则对应线索")
    @RequestMapping(path = "/pageClueByRule", method = RequestMethod.POST)
    public ResponseEntity<Object> pageClueByRule(@RequestHeader(name = "Authorization", defaultValue = "") String auth, @RequestBody Map<String, Object> param) {
        return ksRuleServiceOld.pageClueByRule(auth, param);
    }

    //    @RequestMapping(path = "/listRuleType", method = RequestMethod.GET)
//    public ResponseEntity<Object> listRuleType() {
//        return ksRuleService.listRuleType();
//    }
    @ApiOperation("查询规则类型列表")
    @RequestMapping(path = "/listRuleType", method = RequestMethod.POST)
    public ResponseEntity<Object> listRuleType(@RequestBody(required = false) List<String> hiddenContainsList) {
        if (hiddenContainsList == null || hiddenContainsList.isEmpty()) {
            return ksRuleServiceOld.listRuleType();
        }
        return ksRuleServiceOld.listRuleType(hiddenContainsList);
    }

    @ApiOperation("通过规则id查询线索")
    @RequestMapping(path = "/listClueResultByRuleId", method = RequestMethod.GET)
    public ResponseEntity<Object> listClueResultByRuleId(Integer ruleId) {
        return ksRuleServiceOld.listClueResultByRuleId(ruleId);
    }

    @ApiOperation("单条数据判断字段是否符合规则需求")
    @RequestMapping(path = "/testFilterData", method = RequestMethod.POST)
    public ResponseEntity<Object> testFilterData(@RequestBody KsRuleServiceOld.FilterDataParam filterDataParam) {
        return ksRuleServiceOld.testFilterData(filterDataParam);
    }
    @ApiOperation("单条数据判断是否符合规则")
    @RequestMapping(path = "/filterData", method = RequestMethod.POST)
    public ResponseEntity<Object> filterData(@RequestBody KsRuleServiceOld.FilterDataParam filterDataParam) {
        return ksRuleServiceOld.filterData(filterDataParam);
    }

    @ApiOperation("批量判断数据是否符合规则")
    @RequestMapping(path = "/batchFilterData", method = RequestMethod.POST)
    public ResponseEntity<Object> batchFilterData(@RequestBody List<KsRuleServiceOld.FilterDataParam> filterDataParams) {
        List<Object> res = new ArrayList<>();
        filterDataParams.forEach(i -> {
            res.add(ksRuleServiceOld.filterData(i).getBody());
        });
        return ResponseEntity.ok(res);
    }

    @ApiOperation("批量判断数据是否符合规则")
    @RequestMapping(path = "/batchFilterDataByTableName", method = RequestMethod.POST)
    public ResponseEntity<Object> batchFilterDataByTableName(@RequestBody List<KsRuleServiceOld.FilterDataParam> filterDataParams) {
        List<Object> res = new ArrayList<>();
        filterDataParams.forEach(i -> {
            res.add(ksRuleServiceOld.filterData(i).getBody());
        });
        return ResponseEntity.ok(res);
    }

    @ApiOperation("更新规则是否有效")
    @GetMapping("/updateRuleAvailable")
    public ResponseEntity updateRuleAvailable(Integer ruleId, Integer available) {
        return ksRuleServiceOld.updateRuleAvailable(ruleId, available);
    }

}
